package com.jm.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jm.hosp.repository.DepartmentRepository;
import com.jm.hosp.service.DepartmentService;
import com.jm.model.hosp.Department;
import com.jm.vo.hosp.DepartmentQueryVo;
import com.jm.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentRepository departmentRepository;

    //上传科室接口
    @Override
    public void save(Map<String, Object> paramMap) {
        String jsonString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(jsonString, Department.class);

        //判断是更新还是修改
        Department targetDepartment = departmentRepository
                .getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if (targetDepartment != null) {//mongodb中存在,修改
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {//mongodb中不存在,添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    //查询科室接口
    @Override
    public Page<Department> findPageDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        //设置Pageable对象 0为第一页
        Pageable pageable = PageRequest.of(page - 1, limit);
        //设置example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);

        Page<Department> pageList = departmentRepository.findAll(example, pageable);
        return pageList;
    }

    //删除科室接口
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    //根据医院编号，查询医院所有科室
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        //根据编号，查询科室
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据大科室编号bigcode，进行分组
        Map<String, List<Department>> collect =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历collect，封装每一个大科室信息
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            //获取大科室编号
            String bigCode = entry.getKey();
            //获取大科室下的所有小科室
            List<Department> departmentList1 = entry.getValue();
            //封装每一个大科室
            DepartmentVo departmentVo = new DepartmentVo();
            //大科室的编号、名称
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(departmentList1.get(0).getBigname());
            //大科室下的children信息
            List<DepartmentVo> departmentVoList = new ArrayList<>();
            for (Department department : departmentList1) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department.getDepcode());
                departmentVo1.setDepname(department.getDepname());
                //添加到children集合中
                departmentVoList.add(departmentVo1);
            }
            //把小科室list放到大科室children中
            departmentVo.setChildren(departmentVoList);
            //把大科室放到result中
            result.add(departmentVo);
        }
        //返回
        return result;
    }

    //根据医院编号和科室编号，查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return department.getDepname();
        }
        return null;
    }

    //根据医院编号和科室编号,获取科室
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }
}
